package com.intellagric.agriecom.controller.agriecom_index;

import com.intellagric.agriecom.module.agriecom_produce.ProduceService;
import com.intellagric.agriecom.module.agriecom_produce_category.AgriecomProduceCategoryService;
import com.intellagric.agriecom.module.agriecom_quotation.QuotationService;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.AgriecomProduce;
import com.intellagric.pojo.AgriecomProduceCategory;
import com.intellagric.pojo.AgriecomQuotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LatestInfoController {

    @Autowired
    public QuotationService quotationService;

    @Autowired
    public ProduceService produceService;

    @Autowired
    public AgriecomProduceCategoryService categoryService;


    @RequestMapping("/agriecomIndex/LatestInfo/latestQuotation")
    @ResponseBody
    public List<AgriecomQuotation> latestQuotation(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value="limit",defaultValue = "50") int rows){
        List<AgriecomQuotation> list=quotationService.getQuotationList(page,rows).getData();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        for(AgriecomQuotation quotation:list){
            String[] strs=quotation.getTrend().split(",");
            //返回价格趋势，若长度不等于七则不显示（系统出错）
            try {
                quotation.setRecordTime(format.parse(format.format(quotation.getRecordTime())));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if(strs.length==7){
                if(Float.parseFloat(strs[6])-Float.parseFloat(strs[5])>0){
                    quotation.setTrend("↑");
                }else if(Float.parseFloat(strs[6])-Float.parseFloat(strs[5])<0){
                    quotation.setTrend("↓");
                }else
                    quotation.setTrend("-");
            }else
                quotation.setTrend("-");
        }
        return list;
    }

    /**
     * 返回产品相关信息：价格，名字，分类名，单位等 根据产品id
     * @param productId
     * @return
     */
    @RequestMapping("/agriecomIndex/LatestInfo/proAndCategoryName")
    @ResponseBody
    public ResponseMessage proAndCategoryName(@RequestParam(value = "id") String productId) {
        Map<String,Object> result=new HashMap<>();
        ResponseMessage msg = produceService.selectProduceById(productId);
         if(msg.getData().get("produce")!=null&&!msg.getData().get("produce").equals("")) {
             AgriecomProduce product = (AgriecomProduce) msg.getData().get("produce");//获取产品
             AgriecomProduceCategory category=categoryService.getProduceCategoryById(product.getCategoryId());
             if(category!=null) {
                 result.put("categoryName", category.getName());    //获取产品分类名
                 result.put("productName", product.getProduceName());
                 result.put("price",product.getPrice());
                 result.put("unit",product.getUnit());
                 ResponseMessage responseMessage = new ResponseMessage();
                 responseMessage.setData(result);
                 responseMessage.setMessage("success");
                 return responseMessage;
             }else
                 return ResponseMessage.fail();
         }
        return ResponseMessage.fail();
    }

    }
